package com.maxd.modules.selfshop.merchants.controller;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.selfshop.merchants.entity.SelfMerchantApply;
import com.maxd.modules.selfshop.merchants.service.SelfMerchantApplyService;
import com.maxd.service.money.IMoneyDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@Api(value="自营商城多商户商户入驻",tags={"自营商城多商户商户入驻"})
@RequestMapping(value = "/self/merchantApply")
public class SelfMerchantApplyController {
    @Autowired
    private SelfMerchantApplyService service;
    @Autowired
    private IMoneyDetailsService iMoneyDetailsService;

    @IgnoreSecurity
    @GetMapping("/list")
    @ApiOperation("商户列表")
    public Result findAll(Integer page, Integer size,
                          @ApiParam("店铺名称") @RequestParam(required = false) String storeName,
                          @ApiParam("注册手机号") @RequestParam(required = false) String legalPhone,
                          @ApiParam("店铺类型（0全部 1个人 2个体工商户 3企业 4其他组织）") @RequestParam Integer storeType,
                          @ApiParam("审核状态（0全部 1待处理 2通过 3拒绝）") @RequestParam Integer status,
                          @ApiParam("申请时间开始区间") @RequestParam(required = false) String createTimeStart,
                          @ApiParam("申请时间结束区间") @RequestParam(required = false) String createTimeEnd) {
        return service.findAll(page, size, storeName, legalPhone, storeType, status, createTimeStart, createTimeEnd);
    }

    @IgnoreSecurity
    @GetMapping("/find")
    @ApiOperation("查询")
    public Result findOne(Long id) {
        return service.findOne(id);
    }

    @IgnoreSecurity
    @PostMapping("/save")
    @ApiOperation("添加")
    public Result saveBody(@RequestBody SelfMerchantApply entity) {
        return service.saveBody(entity);
    }

    @IgnoreSecurity
    @PostMapping("/update")
    @ApiOperation("修改")
    public Result updateBody(@RequestBody SelfMerchantApply entity) {
        return service.updateBody(entity);
    }

    @GetMapping("/delete")
    @ApiOperation("删除")
    public Result delete(Long id) {
        return service.delete(id);
    }

    @IgnoreSecurity
    @PostMapping("/deal")
    @ApiOperation("处理审核")
    public Result deal(@ApiParam("申请id") @RequestParam Long id,
                       @ApiParam("审核状态（1待处理 2通过 3拒绝）") @RequestParam Integer status, BigDecimal rate,
                       @ApiParam("拒绝原因") @RequestParam(required = false) String refundReason) {
        return service.deal(id, status,rate, refundReason);
    }

    @IgnoreSecurity
    @GetMapping("/reviewCode")
    @ApiOperation("校验手机号发送验证码")
    public Result reviewCode(String phone) {
        return service.reviewCode(phone);
    }

    @IgnoreSecurity
    @GetMapping("/review")
    @ApiOperation("校验手机号补充资料")
    public Result review(String phone, String msg) {
        return service.review(phone, msg);
    }

    @IgnoreSecurity
    @GetMapping("/updateZhiFuBao")
    @ApiOperation("修改商户支付宝信息")
    public Result updateZhiFuBao(Long merchantId, String zhiFuBaoAccount, String zhiFuBaoName) {
        return service.updateZhiFuBao(merchantId, zhiFuBaoAccount, zhiFuBaoName);
    }

    @IgnoreSecurity
    @GetMapping("/updateWx")
    @ApiOperation("修改商户微信信息")
    public Result updateWx(Long merchantId, String wxImg) {
        return service.updateWx(merchantId, wxImg);
    }

    @RequestMapping(value = "/selectDetails", method = RequestMethod.GET)
    @ApiOperation("获取商户提现记录")
    @ResponseBody
    public Result selectUserDetails(int page,int size,Long merchantId,Integer classify) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page, size, Sort.by(order));
        return ResultUtil.success(iMoneyDetailsService.selectMoneyDetailsByMerchantId(pageable,merchantId,classify));
    }

}
